<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑</title>

    <!-- 引入自己写的样式 -->
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_edit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/tools.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>  
    <a href="blog_edit.html">写博客</a>
    <a href="javascript:void(0)" onclick="onExit()">退出登陆</a>
</div>
<!-- 编辑框容器 -->
<div class="blog-edit-container">
    <!-- 标题编辑区 -->
    <div class="title" id="draft">
        <input id="title" type="text" placeholder="在这里写下文章标题">
        <button onclick="draftsub()">修改草稿</button>
        <button onclick="mysub()">发布文章</button>
    </div>
    <!-- 创建编辑器标签 -->
    <div id="editorDiv">
        <textarea id="editor-markdown" style="display:none;"></textarea>
    </div>
</div>

<script>
    //登录凭证
    var token=sessionStorage.getItem("token");

    var aid;
    var editor;
    function initEdit(md){
        // 编辑器设置
        editor = editormd("editorDiv", {
            // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉.
            width: "100%",
            // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
            height: "calc(100% - 50px)",
            // 编辑器中的初始内容
            markdown: md,
            // 指定 editor.md 依赖的插件路径
            path: "editor.md/lib/",
            saveHTMLToTextarea: true //
        });
    }
    // 提交
    function draftsub(){
        var title = jQuery("#title");
        var content = editor.getValue();
        // 非空效验
        if(title.val()==""){
            title.focus();
            alert("请先输入标题！");
            return false;
        }
        if(content==""){
            title.focus();
            alert("请先输入正文！");
            return false;
        }
        jQuery.ajax({
            url:"/draft/update",
            type:"POST",
            beforeSend: function(request) {
                            request.setRequestHeader("authorization",token);
            },
            data:{
                "aid":aid,
                "title":title.val(),
                "content":content
            },
            success:function(result){
                if(result.code==200 && result.data>0){
                    alert("修改成功");
                    location.href = "myblog_draftlist.html";
                }else{
                    alert("抱歉：修改失败，请重试！");
                }
            },
            error:function(xhr){
                if(xhr.status==401){
                    alert("用户未登录，即将跳转到登录页！");
                    location.href = "/login.html";
                }
            }
        });
    }

    function mysub(){
        var title = jQuery("#title");
        var content = editor.getValue();
        // 非空效验
        if(title.val()==""){
            title.focus();
            alert("请先输入标题！");
            return false;
        }
        if(content==""){
            title.focus();
            alert("请先输入正文！");
            return false;
        }
        jQuery.ajax({
            url:"/draft/submit",
            type:"POST",
            beforeSend: function(request) {
                            request.setRequestHeader("authorization",token);
            },
            data:{
                "aid":aid,
                "title":title.val(),
                "content":content
            },
            success:function(result){
                if(result.code==200 && result.data>0){
                    alert("修改成功");
                    location.href = "myblog_draftlist.html";
                }else{
                    alert("抱歉：修改失败，请重试！");
                }
            },
            error:function(xhr){
                if(xhr.status==401){
                    alert("用户未登录，即将跳转到登录页！");
                    location.href = "/login.html";
                }
            }
        });
    }

    // 查询文章详情并展现
    function showArt(){
        // 从 url 中获取文章 id
        aid=getURLParam("id");
        if(aid!=null && aid>0){
            //后端查询文章详情
            jQuery.ajax({
                url:"/draft/detail",
                type:"POST",
                beforeSend: function(request) {
                            request.setRequestHeader("authorization",token);
                },
                data:{"aid":aid},
                success:function(result){
                    if(result.code==200 && result.data!=null){
                        var art = result.data;
                        jQuery("#title").val(art.title);
                        initEdit(art.content);
                    }else{
                        alert("查询失败，请重试！");
                    }
                },
                error:function(xhr){
                    if(xhr.status==401){
                        alert("用户未登录，即将跳转到登录页！");
                        location.href = "/login.html";
                    }
                }
            });
        }
    }
    showArt();
</script>
</body>

</html>